package by.yakusekmaxim.bookgid.webapp.page.admin.user;

import javax.inject.Inject;

import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.ResourceModel;

import by.yakusekmaxim.bookgid.dataaccess.util.UserAccountFilter;
import by.yakusekmaxim.bookgid.datamodel.UserAccount;
import by.yakusekmaxim.bookgid.datamodel.UserAccount_;
import by.yakusekmaxim.bookgid.datamodel.enums.UserRole;
import by.yakusekmaxim.bookgid.services.UserAccountService;
import by.yakusekmaxim.bookgid.webapp.page.AdminLayout;

public class UserPage extends AdminLayout {

	@Inject
	private UserAccountService userAccountService;

	private UserAccountFilter filter = new UserAccountFilter();

	protected void onInitialize() {
		super.onInitialize();

		
		Link<Object> countAll = (new Link<Object>("linkCountAll"){

			@Override
			public void onClick() {
				UserPage userPage = new UserPage();
				setResponsePage(userPage);
			}
		});
		countAll.setEnabled(userAccountService.getCount() != 0);
		countAll.add(new Label("countAll", userAccountService.getCount()));
		add(countAll);
		
		
		Link<Object> countUsers = (new Link<Object>("linkCountUsers"){

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(UserAccount_.role);
				filter.setValue(UserRole.userrole_user);
				UserPage userPage = new UserPage(filter, new ResourceModel("p.admin.user.pageName.user"));
				setResponsePage(userPage);
			}
		});
		countUsers.setEnabled(userAccountService.getCount(
				UserAccount_.role, UserRole.userrole_user) != 0);
		countUsers.add(new Label("countUsers", userAccountService.getCount(
				UserAccount_.role, UserRole.userrole_user)));
		add(countUsers);
		
		
		Link<Object> countEditor = (new Link<Object>("linkCountEditor"){

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(UserAccount_.role);
				filter.setValue(UserRole.userrole_editor);
				UserPage userPage = new UserPage(filter, new ResourceModel("p.admin.user.pageName.editor"));
				setResponsePage(userPage);
			}
		});
		countEditor.setEnabled(userAccountService.getCount(
				UserAccount_.role, UserRole.userrole_editor) != 0);
		countEditor.add(new Label("countEditor", userAccountService.getCount(
				UserAccount_.role, UserRole.userrole_editor)));
		add(countEditor);
		
		Link<Object> countAdmin = (new Link<Object>("linkCountAdmin"){

			@Override
			public void onClick() {
				filter.setEnabled(true);
				filter.setAttribute(UserAccount_.role);
				filter.setValue(UserRole.userrole_admin);
				UserPage userPage = new UserPage(filter, new ResourceModel("p.admin.user.pageName.admin"));
				setResponsePage(userPage);
			}
		});
		countAdmin.setEnabled(userAccountService.getCount(
				UserAccount_.role, UserRole.userrole_admin) != 0);
		countAdmin.add(new Label("countAdmin", userAccountService.getCount(
				UserAccount_.role, UserRole.userrole_admin)));
		add(countAdmin);
		
		add(new Link<Object>("linkEditUserPage") {
			@Override
			public void onClick() {
				setResponsePage(new EditUserPage(new UserAccount()));
			}
		});
		
	}

	public UserPage() {
		filter.setEnabled(false);
		add(new Label("pageNameLabel", getPageTitle()));
		add(new UserListPanel("userList", filter));
	}

	public UserPage(UserAccountFilter filter,ResourceModel pageName) {
		add(new Label("pageNameLabel", pageName));
		add(new UserListPanel("userList", filter));
	}

	@Override
	protected IModel<String> getPageTitle() {
		return new ResourceModel("adminmenu.user's");
	}

	@Override
	protected WebMarkupContainer getTarget() {
		return targetUser;
	}

}
